cc.Class({
  extends: cc.Component,

  properties: {
    dataEventHandler: null,
    roomId: null,
    maxNumOfGames: -1,
    numOfGames: -1,
    numOfMJ: 0,
    seatIndex: -1,
    seats: null,
    turn: -1,
    button: -1,
    dingque: -1,
    chupai: -1,
    isDingQueing: false,
    isHuanSanZhang: false,
    gamestate: "",
    isOver: false,
    dissoveData: null,

    //舟山麻将额外属性
    fengxiang: null

    // foo: {
    //    default: null,
    //    url: cc.Texture2D,  // optional, default is typeof default
    //    serializable: true, // optional, default is true
    //    visible: true,      // optional, default is true
    //    displayName: 'Foo', // optional
    //    readonly: false,    // optional, default is false
    // },
    // ...
  },

  reset: function() {
    this.turn = -1;
    (this.chupai = -1), (this.dingque = -1);
    this.button = -1;
    this.gamestate = "";
    this.dingque = -1;
    this.isDingQueing = false;
    this.isHuanSanZhang = false;
    this.curaction = null;
    for (var i = 0; i < this.seats.length; ++i) {
      this.seats[i].holds = [];
      this.seats[i].folds = [];
      this.seats[i].pengs = [];
      this.seats[i].chis = [];
      this.seats[i].huas = [];
      this.seats[i].angangs = [];
      this.seats[i].diangangs = [];
      this.seats[i].wangangs = [];
      this.seats[i].dingque = -1;
      this.seats[i].ready = false;
      this.seats[i].hued = false;
      this.seats[i].huanpais = null;
      this.huanpaimethod = -1;
    }
  },

  clear: function() {
    this.dataEventHandler = null;
    if (this.isOver == null) {
      this.seats = null;
      this.roomId = null;
      this.maxNumOfGames = 0;
      this.numOfGames = 0;
    }
  },

  dispatchEvent(event, data) {
    if (this.dataEventHandler) {
      this.dataEventHandler.emit(event, data);
    }
  },

  getSeatIndexByID: function(userId) {
    for (var i = 0; i < this.seats.length; ++i) {
      var s = this.seats[i];
      if (s.userid == userId) {
        return i;
      }
    }
    return -1;
  },

  isOwner: function() {
    return this.seatIndex == 0;
  },

  getSeatByID: function(userId) {
    var seatIndex = this.getSeatIndexByID(userId);
    var seat = this.seats[seatIndex];
    return seat;
  },

  getSelfData: function() {
    return this.seats[this.seatIndex];
  },

  getLocalIndex: function(index) {
    var ret = (index - this.seatIndex + 4) % 4;
    return ret;
  },

  prepareReplay: function(roomInfo, detailOfGame) {
    this.roomId = roomInfo.id;
    this.seats = roomInfo.seats;
    this.turn = detailOfGame.base_info.button;
    var baseInfo = detailOfGame.base_info;
    for (var i = 0; i < this.seats.length; ++i) {
      var s = this.seats[i];
      s.seatindex = i;
      s.score = null;
      s.holds = baseInfo.game_seats[i];
      s.pengs = [];
      s.chis = [];
      s.huas = [];
      s.angangs = [];
      s.diangangs = [];
      s.wangangs = [];
      s.folds = [];
      if (cc.vv.userMgr.userId == s.userid) {
        this.seatIndex = i;
      }
    }
    this.conf = {
      type: baseInfo.type
    };
    if (this.conf.type == null) {
      this.conf.type == "sjmmmj";
    }
  },

  getWanfa: function() {
    var conf = this.conf;
    if (conf && conf.maxGames != null && conf.maxFan != null) {
      var strArr = [];
      // strArr.push(conf.maxGames + "局");
      // strArr.push(conf.maxFan + "番封顶");
      if (conf.type == "sjmmj") {
        conf.koufei == 0 ? strArr.push("房主出资") : strArr.push("玩家平分");
        conf.hongzhongdanghua ? strArr.push("红中当花") : {};
        conf.quanshu == 0 ? strArr.push("8局") : strArr.push("一圈");
        if (conf.jiesuan == 0) strArr.push("幺半");
        else if (conf.jiesuan == 1) strArr.push("一二");
        else if (conf.jiesuan == 2) strArr.push("二四");
      } else if (conf.type == "dhmj") {
        conf.koufei == 0 ? strArr.push("房主出资") : strArr.push("玩家平分");
        conf.quanshu == 0 ? strArr.push("8局") : strArr.push("一圈");
        if (conf.jiesuan == 0) strArr.push("10");
        else if (conf.jiesuan == 1) strArr.push("25");
        else if (conf.jiesuan == 2) strArr.push("50");
        else if (conf.jiesuan == 3) strArr.push("120");
      } else if (conf.type == "tdh") {
        conf.koufei == 0 ? strArr.push("房主出资") : strArr.push("玩家平分");
        conf.quanshu == 0 ? strArr.push("8局") : strArr.push("一圈");
        if (conf.jiesuan == 0) strArr.push("有花");
      }
      // if(conf.zimo == 1){
      //     strArr.push("自摸加番");
      // }
      // else{
      //     strArr.push("自摸加底");
      // }
      // if(conf.jiangdui){
      //     strArr.push("将对");
      // }
      // if(conf.dianganghua == 1){
      //     strArr.push("点杠花(自摸)");
      // }
      // else{
      //     strArr.push("点杠花(放炮)");
      // }
      // if(conf.menqing){
      //     strArr.push("门清、中张");
      // }
      // if(conf.tiandihu){
      //     strArr.push("天地胡");
      // }
      return strArr.join(" ");
    }
    return "";
  },

  initHandlers: function() {
    var self = this;
    cc.vv.net.addHandler("login_result", function(data) {
      console.log("login_result");
      console.log(data);
      if (data.errcode === 0) {
        var data = data.data;
        self.roomId = data.roomid;
        self.conf = data.conf;
        self.maxNumOfGames = data.conf.maxGames;
        self.numOfGames = data.numofgames;
        self.fengxiang = data.fengxiang;
        self.seats = data.seats;
        self.seatIndex = self.getSeatIndexByID(cc.vv.userMgr.userId);
        self.isOver = false;
      } else {
        console.log(data.errmsg);
      }
    });

    cc.vv.net.addHandler("login_finished", function(data) {
      console.log("login_finished", data);

      cc.director.loadScene("mjgame");

      // var loadgame = function (){
      //     cc.director.loadScene("mjgame");
      // }
      // var fadeout = cc.fadeOut(0.1);
      // var finish = cc.callFunc(loadgame, this);
      // var seq = cc.sequence(fadeout, finish);
      // cc.director.getScene().getChildByName('Canvas').runAction(seq);

      //cc.director.loadScene("mjgame");
    });

    cc.vv.net.addHandler("exit_result", function(data) {
      self.roomId = null;
      self.turn = -1;
      self.dingque = -1;
      self.isDingQueing = false;
      self.seats = null;
    });

    cc.vv.net.addHandler("exit_notify_push", function(data) {
      var userId = data;
      var s = self.getSeatByID(userId);
      if (s != null) {
        s.userid = 0;
        s.name = "";
        self.dispatchEvent("user_state_changed", s);
      }
    });

    cc.vv.net.addHandler("dispress_push", function(data) {
      self.roomId = null;
      self.turn = -1;
      self.dingque = -1;
      self.isDingQueing = false;
      self.seats = null;
    });

    cc.vv.net.addHandler("disconnect", function(data) {
      if (self.roomId == null) {
        cc.director.loadScene("hall");
      } else {
        if (self.isOver == false) {
          cc.vv.userMgr.oldRoomId = self.roomId;
          self.dispatchEvent("disconnect");
        } else {
          self.roomId = null;
        }
      }
    });

    cc.vv.net.addHandler("new_user_comes_push", function(data) {
      //console.log(data);
      var seatIndex = data.seatindex;
      if (self.seats[seatIndex].userid > 0) {
        self.seats[seatIndex].online = true;
      } else {
        data.online = true;
        self.seats[seatIndex] = data;
      }
      self.dispatchEvent("new_user", self.seats[seatIndex]);
    });

    cc.vv.net.addHandler("user_state_push", function(data) {
      //console.log(data);
      var userId = data.userid;
      var seat = self.getSeatByID(userId);
      seat.online = data.online;
      self.dispatchEvent("user_state_changed", seat);
    });

    cc.vv.net.addHandler("user_ready_push", function(data) {
      console.log("debug:user_ready_push", data);
      var userId = data.userid;
      var seat = self.getSeatByID(userId);
      seat.ready = data.ready;
      self.dispatchEvent("user_state_changed", seat);
    });

    cc.vv.net.addHandler("game_holds_push", function(data) {
      console.log("debug:game_holds_push", data);
      var seat = self.seats[self.seatIndex];
      seat.holds = data;

      for (var i = 0; i < self.seats.length; ++i) {
        var s = self.seats[i];
        if (s.folds == null) {
          s.folds = [];
        }
        if (s.pengs == null) {
          s.pengs = [];
        }
        if (s.angangs == null) {
          s.angangs = [];
        }
        if (s.diangangs == null) {
          s.diangangs = [];
        }
        if (s.chis == null) {
          s.chis = [];
        }
        if (s.huas == null) {
          s.huas = [];
        }
        if (s.wangangs == null) {
          s.wangangs = [];
        }
        s.ready = false;
      }
      self.dispatchEvent("game_holds");
    });

    cc.vv.net.addHandler("game_feng_push", function(data) {
      self.fengxiang = data;
      self.dispatchEvent("game_feng");
    });

    cc.vv.net.addHandler("game_begin_push", function(data) {
      console.log("debug:game_begin_push", data);
      self.button = data;
      self.turn = self.button;
      self.gamestate = "begin";
      self.dispatchEvent("game_begin");
    });

    cc.vv.net.addHandler("game_playing_push", function(data) {
      console.log("game_playing_push");
      self.gamestate = "playing";
      self.dispatchEvent("game_playing");
    });

    cc.vv.net.addHandler("game_sync_push", function(data) {
      console.log("debug:game_sync_push", data);
      self.numOfMJ = data.numofmj;
      self.gamestate = data.state;
      self.fengxiang = data.fengxiang;
      if (self.gamestate == "dingque") {
        self.isDingQueing = true;
      } else if (self.gamestate == "huanpai") {
        self.isHuanSanZhang = true;
      }
      self.turn = data.turn;
      self.button = data.button;
      self.chupai = data.chuPai;
      self.huanpaimethod = data.huanpaimethod;
      for (var i = 0; i < 4; ++i) {
        var seat = self.seats[i];
        var sd = data.seats[i];
        seat.holds = sd.holds;
        seat.folds = sd.folds;
        seat.angangs = sd.angangs;
        seat.diangangs = sd.diangangs;
        seat.wangangs = sd.wangangs;
        seat.pengs = sd.pengs;
        seat.chis = sd.chis;
        seat.huas = sd.huas;
        seat.dingque = sd.que;
        seat.hued = sd.hued;
        seat.iszimo = sd.iszimo;
        seat.huinfo = sd.huinfo;
        seat.huanpais = sd.huanpais;
        if (i == self.seatIndex) {
          self.dingque = sd.que;
        }
      }
    });

    cc.vv.net.addHandler("game_huanpai_push", function(data) {
      self.isHuanSanZhang = true;
      self.dispatchEvent("game_huanpai");
    });

    cc.vv.net.addHandler("hangang_notify_push", function(data) {
      self.dispatchEvent("hangang_notify", data);
    });

    cc.vv.net.addHandler("game_action_push", function(data) {
      console.log("debug:game_action_push", data);
      self.curaction = data;
      self.dispatchEvent("game_action", data);
    });

    cc.vv.net.addHandler("game_num_push", function(data) {
      self.numOfGames = data;
      self.dispatchEvent("game_num", data);
    });

    cc.vv.net.addHandler("game_over_push", function(data) {
      console.log("debug:game_over_push:", data);
      var results = data.results;
      for (var i = 0; i < self.seats.length; ++i) {
        self.seats[i].score = results.length == 0 ? 0 : results[i].totalscore;
      }
      self.dispatchEvent("game_over", results);
      if (data.endinfo) {
        self.isOver = true;
        self.dispatchEvent("game_end", data.endinfo);
      }
      self.reset();
      for (var i = 0; i < self.seats.length; ++i) {
        self.dispatchEvent("user_state_changed", self.seats[i]);
      }
    });

    cc.vv.net.addHandler("mj_count_push", function(data) {
      console.log("debug:mj_count_push:", data);
      self.numOfMJ = data;
      self.dispatchEvent("mj_count", data);
    });

    cc.vv.net.addHandler("game_chupai_push", function(data) {
      console.log("debug:game_chupai_push:", data);
      var turnUserID = data;
      var si = self.getSeatIndexByID(turnUserID);
      self.doTurnChange(si);
    });

    cc.vv.net.addHandler("hu_push", function(data) {
      console.log("debug:hu_push", data);
      self.doHu(data);
    });

    cc.vv.net.addHandler("game_chupai_notify_push", function(data) {
      var userId = data.userId;
      var pai = data.pai;
      var si = self.getSeatIndexByID(userId);
      self.doChupai(si, pai);
    });

    cc.vv.net.addHandler("game_mopai_push", function(data) {
      console.log("debug:game_mopai_push", data);
      self.doMopai(self.seatIndex, data);
    });

    cc.vv.net.addHandler("guo_notify_push", function(data) {
      console.log("debug:guo_notify_push", data);
      var userId = data.userId;
      var pai = data.pai;
      var si = self.getSeatIndexByID(userId);
      self.doGuo(si, pai);
    });

    cc.vv.net.addHandler("guo_result", function(data) {
      console.log("debug:guo_result:", data);
      self.dispatchEvent("guo_result");
    });

    cc.vv.net.addHandler("guohu_push", function(data) {
      console.log("guohu_push");
      self.dispatchEvent("push_notice", { info: "过胡", time: 1.5 });
    });

    cc.vv.net.addHandler("huanpai_notify", function(data) {
      var seat = self.getSeatByID(data.si);
      seat.huanpais = data.huanpais;
      self.dispatchEvent("huanpai_notify", seat);
    });

    cc.vv.net.addHandler("game_huanpai_over_push", function(data) {
      console.log("game_huanpai_over_push");
      var info = "";
      var method = data.method;
      if (method == 0) {
        info = "换对家牌";
      } else if (method == 1) {
        info = "换下家牌";
      } else {
        info = "换上家牌";
      }
      self.huanpaimethod = method;
      cc.vv.gameNetMgr.isHuanSanZhang = false;
      self.dispatchEvent("game_huanpai_over");
      self.dispatchEvent("push_notice", { info: info, time: 2 });
    });

    cc.vv.net.addHandler("peng_notify_push", function(data) {
      console.log("debug:peng_notify_push", data);
      var userId = data.userid;
      var pai = data.pai;
      var si = self.getSeatIndexByID(userId);
      self.doPeng(si, data.pai);
    });

    cc.vv.net.addHandler("gang_notify_push", function(data) {
      console.log("debug:gang_notify_push:", data);
      console.log(data);
      var userId = data.userid;
      var pai = data.pai;
      var si = self.getSeatIndexByID(userId);
      self.doGang(si, pai, data.gangtype);
    });

    cc.vv.net.addHandler("chi_notify_push", function(data) {
      console.log("debug:chi_notify_push", data);
      var userId = data.userid;
      var pai = data.pai;
      var chigroup = data.chigroup;
      var si = self.getSeatIndexByID(userId);
      self.doChi(si, data.pai, chigroup);
    });

    cc.vv.net.addHandler("gethua_notify_push", function(data) {
      console.log("debug:gethua_notify_push", data);
      var userId = data.userid;
      var pai = data.pai;
      var si = self.getSeatIndexByID(userId);
      self.doHua(si, data.pai);
    });

    //开局补花侦听 全局
    cc.vv.net.addHandler("buhua_notify_push", function(data) {
      console.log("debug:buhua_notify_push", data);
      var userId = data.userid;
      var buhuas = data.buhuas;
      var si = self.getSeatIndexByID(userId);
      self.doBuhua(si, buhuas);
    });

    //开局补花侦听 自己
    cc.vv.net.addHandler("game_buhua_push", function(data) {
      console.log("debug:game_buhua_push", data);
      var userId = data.userid;
      var holds = data.holds;
      var buhuas = data.buhuas;
      var si = self.getSeatIndexByID(userId);
      self.doBuhuaforme(si, holds, buhuas);
    });

    cc.vv.net.addHandler("game_dingque_notify_push", function(data) {
      self.dispatchEvent("game_dingque_notify", data);
    });

    cc.vv.net.addHandler("game_dingque_finish_push", function(data) {
      for (var i = 0; i < data.length; ++i) {
        self.seats[i].dingque = data[i];
      }
      self.dispatchEvent("game_dingque_finish", data);
    });

    cc.vv.net.addHandler("chat_push", function(data) {
      self.dispatchEvent("chat_push", data);
    });

    cc.vv.net.addHandler("quick_chat_push", function(data) {
      self.dispatchEvent("quick_chat_push", data);
    });

    cc.vv.net.addHandler("emoji_push", function(data) {
      self.dispatchEvent("emoji_push", data);
    });

    cc.vv.net.addHandler("dissolve_notice_push", function(data) {
      console.log("dissolve_notice_push", data);
      self.dissoveData = data;
      self.dispatchEvent("dissolve_notice", data);
    });

    cc.vv.net.addHandler("dissolve_cancel_push", function(data) {
      self.dissoveData = null;
      self.dispatchEvent("dissolve_cancel", data);
    });

    cc.vv.net.addHandler("voice_msg_push", function(data) {
      self.dispatchEvent("voice_msg", data);
    });
  },

  doGuo: function(seatIndex, pai) {
    var seatData = this.seats[seatIndex];
    var folds = seatData.folds;
    folds.push(pai);
    this.dispatchEvent("guo_notify", seatData);
  },

  doMopai: function(seatIndex, pai) {
    var seatData = this.seats[seatIndex];
    if (seatData.holds) {
      seatData.holds.push(pai);
      this.dispatchEvent("game_mopai", { seatIndex: seatIndex, pai: pai });
    }
  },

  doChupai: function(seatIndex, pai) {
    this.chupai = pai;
    var seatData = this.seats[seatIndex];
    if (seatData.holds) {
      var idx = seatData.holds.indexOf(pai);
      seatData.holds.splice(idx, 1);
    }
    this.dispatchEvent("game_chupai_notify", { seatData: seatData, pai: pai });
  },

  doPeng: function(seatIndex, pai) {
    var seatData = this.seats[seatIndex];
    //移除手牌
    if (seatData.holds) {
      for (var i = 0; i < 2; ++i) {
        var idx = seatData.holds.indexOf(pai);
        seatData.holds.splice(idx, 1);
      }
    }

    //更新碰牌数据
    var pengs = seatData.pengs;
    pengs.push(pai);

    this.dispatchEvent("peng_notify", seatData);
  },

  doChi: function(seatIndex, pai, chigroup) {
    console.log("dochi");
    var seatData = this.seats[seatIndex];
    console.log(seatData);
    //移除手牌
    if (seatData.holds) {
      for (var i = 0; i < 3; ++i) {
        if (pai == chigroup[i]) continue;
        var idx = seatData.holds.indexOf(chigroup[i]);
        seatData.holds.splice(idx, 1);
      }
    }

    //更新吃牌数据
    var chis = seatData.chis;
    chis.push(chigroup);

    this.dispatchEvent("chi_notify", seatData);
  },

  doHua: function(seatIndex, pai) {
    var seatData = this.seats[seatIndex];
    //更新花牌数据
    if (seatData.huas == undefined || seatData.huas == null) {
      seatData.huas = [];
    }
    var huas = seatData.huas;
    huas.push(pai);
    this.dispatchEvent("gethua_notify", seatData);
  },

  doBuhua: function(seatIndex, buhuas) {
    console.log("debug:seatIndex:", seatIndex, buhuas);
    var seatData = this.seats[seatIndex];
    //更新花牌数据
    if (seatData.huas == undefined || seatData.huas == null) {
      seatData.huas = [];
    }
    var huas = seatData.huas;
    for (var i = 0; i < buhuas.length; i++) {
      huas.push(buhuas[i]);
    }
    this.dispatchEvent("gethua_notify", seatData);
  },

  doBuhuaforme: function(seatIndex, holds, buhuas) {
    console.log("da buhua");
    var seatData = this.seats[seatIndex];
    // for(var i=0 ; i < holds.length ; i++) {
    //     seatData.holds.push(holds[i]);
    // }
    // for(var i=0 ; i < buhuas.length ; i++) {
    //     var idx = seatData.holds.indexOf(buhuas[i]);
    //     seatData.holds.splice(idx,1);
    // }
    seatData.holds = holds;
    this.dispatchEvent("buhua_notify", seatData);
  },

  getGangType: function(seatData, pai) {
    if (seatData.pengs.indexOf(pai) != -1) {
      return "wangang";
    } else {
      var cnt = 0;
      for (var i = 0; i < seatData.holds.length; ++i) {
        if (seatData.holds[i] == pai) {
          cnt++;
        }
      }
      if (cnt == 3) {
        return "diangang";
      } else {
        return "angang";
      }
    }
  },

  doGang: function(seatIndex, pai, gangtype) {
    var seatData = this.seats[seatIndex];

    if (!gangtype) {
      gangtype = this.getGangType(seatData, pai);
    }

    if (gangtype == "wangang") {
      if (seatData.pengs.indexOf(pai) != -1) {
        var idx = seatData.pengs.indexOf(pai);
        if (idx != -1) {
          seatData.pengs.splice(idx, 1);
        }
      }
      seatData.wangangs.push(pai);
    }
    if (seatData.holds) {
      for (var i = 0; i <= 4; ++i) {
        var idx = seatData.holds.indexOf(pai);
        if (idx == -1) {
          //如果没有找到，表示移完了，直接跳出循环
          break;
        }
        seatData.holds.splice(idx, 1);
      }
    }
    if (gangtype == "angang") {
      seatData.angangs.push(pai);
    } else if (gangtype == "diangang") {
      seatData.diangangs.push(pai);
    }
    this.dispatchEvent("gang_notify", {
      seatData: seatData,
      gangtype: gangtype
    });
  },

  doHu: function(data) {
    this.dispatchEvent("hupai", data);
  },

  doTurnChange: function(si) {
    var data = {
      last: this.turn,
      turn: si
    };
    this.turn = si;
    this.dispatchEvent("game_chupai", data);
  },

  connectGameServer: function(data) {
    this.dissoveData = null;
    cc.vv.net.ip = data.ip + ":" + data.port;
    console.log(cc.vv.net.ip);
    var self = this;

    var onConnectOK = function() {
      console.log("onConnectOK");
      var sd = {
        token: data.token,
        roomid: data.roomid,
        time: data.time,
        sign: data.sign
      };
      cc.vv.net.send("login", sd);
    };

    var onConnectFailed = function() {
      console.log("failed.");
      cc.vv.wc.hide();
    };
    cc.vv.wc.show("正在进入房间");
    cc.vv.net.connect(
      onConnectOK,
      onConnectFailed
    );
  }

  // called every frame, uncomment this function to activate update callback
  // update: function (dt) {

  // },
});
